//
//  XMNetwork+login.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/9.
//

#import "XMNetwork+login.h"

@implementation XMNetwork (login)

/**
 本机号码一键登录
 */
- (void)loginWithPhoneNum:(NSString *)phoneNumStr
                  success:(void (^)(id _Nonnull))successBlock
                  failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:phoneNumStr forKey:@"mobile"];
    [self postDataWithPath:loginWithLocalPhone
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 获取验证码
 */
- (void)getCodeWithPhoneNum:(NSString *)phoneNumStr
                       type:(nonnull NSString *)codeType
                    success:(void (^)(id _Nonnull))successBlock
                    failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:phoneNumStr forKey:@"mobile"];
    [parameters safe_setObject:codeType forKey:@"scene"];
    [self postDataWithPath:getPhoneCode
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 校验验证码
 */
- (void)valieCodeWithPhoneNum:(NSString *)phoneNum
                      codeStr:(nonnull NSString *)codeStr
                      success:(void (^)(id _Nonnull))successBlock
                      failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:phoneNum forKey:@"mobile"];
    [parameters safe_setObject:codeStr forKey:@"code"];
    [parameters safe_setObject:@"1" forKey:@"scene"];
    [self postDataWithPath:valiateCode
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 手机号验证码登录
 */
- (void)loginWithPhone:(NSString *)phoneNum
                  code:(NSString *)codeStr
               success:(void (^)(id _Nonnull))successBlock
               failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:phoneNum forKey:@"mobile"];
    [parameters safe_setObject:codeStr forKey:@"code"];
    [self postDataWithPath:loginWithCodePhone
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 绑定手机号
 */
- (void)bindPhoneWithCode:(NSString *)codeStr
                  success:(void (^)(id _Nonnull))successBlock
                  failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [self putDataWithPath:bindPhoneUrl
               parameters:parameters
                needToken:NO
                  headers:@{}
          showPrograssHUD:YES
                  success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 获取微信信息
 */
- (void)getWechatInfoWithRespcode:(NSString *)respcode
                          success:(void (^)(id _Nonnull))successBlock
                          failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSString *pathStr = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxe27fd4207b87851f&secret=64c935a441a1d5af024f9a6a1c145ac9&code=%@&grant_type=authorization_code", respcode];
    [self getDataWithPath:pathStr
               parameters:[NSMutableDictionary dictionary]
                needToken:NO
                  headers:@{}
                  success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 微信登陆
 */
- (void)wechatLoginWithCode:(NSString *)codeStr
                    success:(void (^)(id _Nonnull))successBlock
                    failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSString *dateStr = [[NSDate date] dateStringWithFormat:@"yyyy_MM_dd_HH_mm_ss"];
    NSString *str = [NSString stringWithFormat:@"xmjj_zmy_%@_wechat", dateStr];

    [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"wechat_state"];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:@"30" forKey:@"type"];
    [parameters safe_setObject:codeStr forKey:@"code"];
    [parameters safe_setObject:str forKey:@"state"];
    [self postDataWithPath:loginWithWechatUrl
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 判断微信是否绑定手机号
 */
- (void)wechatHasBindPhoneWithAccessToken:(NSString *)accessToken
                                   openId:(NSString *)openId
                                  unionId:(NSString *)unionId
                                  success:(void (^)(id _Nonnull))successBlock
                                  failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:accessToken forKey:@"accessToken"];
    [parameters safe_setObject:openId forKey:@"openid"];
    [parameters safe_setObject:unionId forKey:@"unionid"];
    [self postDataWithPath:wechatHasBindPhoneUrl
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 微信登陆
 */
- (void)wechatLoginWithAccessToken:(NSString *)accessToken
                            openId:(NSString *)openId
                           unionId:(NSString *)unionId
                             phone:(NSString *)phone
                           success:(void (^)(id _Nonnull))successBlock
                           failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:accessToken forKey:@"accessToken"];
    [parameters safe_setObject:openId forKey:@"openid"];
    [parameters safe_setObject:unionId forKey:@"unionid"];
    [parameters safe_setObject:phone forKey:@"phone"];
    [self postDataWithPath:wechatLoginUrl
                parameters:parameters
                 needToken:NO
                   headers:@{}
                   success:^(id  _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

/**
 隐私政策
 */
- (void)getServiceInfoWithType:(NSString *)type
                       success:(void (^)(id _Nonnull))successBlock
                       failure:(void (^)(NSError * _Nonnull))failureBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:type forKey:@"type"];
    [self getDataWithPath:getReturnRuleUrl
               parameters:parameters
                needToken:NO
                  headers:@{}
                  success:^(id _Nonnull responseObject) {
        successBlock (responseObject);
    } failure:^(NSError * _Nonnull error) {
        failureBlock (error);
    }];
}

@end
